/**
 *
 */

package presentation;

import interfaces.GUIVisitor;
import interfaces.UI;
import interfaces.UserConnection;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.net.UnknownHostException;
import java.util.Vector;
import javax.swing.*;
import javax.swing.border.LineBorder;
import com.cloudgarden.layout.AnchorLayout;
import presentation.LoginForm;
import presentation.PostView;
import presentation.WsepGUI;
import domain.WsepConnection;
import domain.items.Item;
import domain.items.PostPacket;
import domain.items.ThreadPacket;
import domain.items.TopicPacket;
import domain.users.Guest;
import domain.users.Member;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
/**
 * here we have the main method (which only invokes the UI).
 * @author awarmanX
 *
 */
public class WsepGUI extends JFrame implements GUIVisitor,UI {

	private static final long serialVersionUID = -3742212102537330109L;
	private UserConnection _connection;
	protected JPanel statusBar;
	protected JLabel logo;
	protected JPanel optionsBar;
	private JLabel usersStatus;
	private JButton LogoffButton;
	protected JButton updateUserButton;
	protected JButton addTopicButton;
	protected JButton deleteUserButton;
	protected JButton promoteButton;
	protected JButton demoteButton;
	protected JButton replyButton;
	protected JButton newMsgButton;
	protected JButton searchButton;
	protected JButton backButton;
	protected JPanel navagationBar;
	protected JPanel itemsContainer;
	protected JScrollPane mainView;
	protected JButton registerButton;
	protected JButton loginButton;
	protected JLabel userName;
	protected JLabel loggedAs;

	/**
	 * Constructor
	 */
	public WsepGUI() {
		super();
		initGUI();
		try {
			this._connection = new WsepConnection(new Guest());

		} catch (IOException e) {
		}
	}


	private void initGUI() {
		try {
			GridBagLayout thisLayout = new GridBagLayout();
			thisLayout.rowWeights = new double[] {0.0, 0.1, 0.0, 0.0};
			thisLayout.rowHeights = new int[] {91, 7, 239, 46};
			thisLayout.columnWeights = new double[] {0.0, 0.1, 0.1, 0.1};
			thisLayout.columnWidths = new int[] {122, 7, 7, 7};
			getContentPane().setLayout(thisLayout);
			setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
			this.setPreferredSize(new java.awt.Dimension(640, 640));
			this.setResizable(false);
			this.setTitle("Power Rangers Forum");
			{
				statusBar = new JPanel();
				GridBagLayout jPanel1Layout = new GridBagLayout();
				jPanel1Layout.columnWidths = new int[] {102, 7};
				jPanel1Layout.rowHeights = new int[] {78, 28};
				jPanel1Layout.columnWeights = new double[] {0.0, 0.1};
				jPanel1Layout.rowWeights = new double[] {0.0, 0.0};
				getContentPane().add(statusBar, new GridBagConstraints(0, 0, 4, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				statusBar.setLayout(jPanel1Layout);
				{
					logo = new JLabel();
					statusBar.add(logo, new GridBagConstraints(0, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					logo.setText("Power Rangers Forum!");
					logo.setFont(new java.awt.Font("Tahoma",0,48));
				}
				{
					loggedAs = new JLabel();
					@SuppressWarnings("unused")
					AnchorLayout loggedAsLayout = new AnchorLayout();
					statusBar.add(loggedAs, new GridBagConstraints(0, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					loggedAs.setText("Logged As : ");
					loggedAs.setLayout(null);
					loggedAs.setFont(new java.awt.Font("Tahoma",0,18));
				}
				{
					userName = new JLabel();
					statusBar.add(userName, new GridBagConstraints(1, 1, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.NONE, new Insets(0, 0, 0, 0), 0, 0));
					userName.setText("Guest");
					userName.setFont(new java.awt.Font("Tahoma",0,18));
				}
			}
			{
				optionsBar = new JPanel();
				GridBagLayout jPanel2Layout = new GridBagLayout();
				jPanel2Layout.columnWidths = new int[] {63, 7, 7};
				jPanel2Layout.rowHeights = new int[] {63, 7, 38, 7, 39, 39, 7, 39, 7, 39, 7, 39, 7, 39, 7, 39, 7, 20, 20};
				jPanel2Layout.columnWeights = new double[] {0.0, 0.1, 0.1};
				jPanel2Layout.rowWeights = new double[] {0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.1, 0.1};
				getContentPane().add(optionsBar, new GridBagConstraints(0, 1, 1, 6, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				optionsBar.setLayout(jPanel2Layout);
				optionsBar.setBorder(new LineBorder(new java.awt.Color(0,0,0), 1, false));
				{
					loginButton = new JButton();
					@SuppressWarnings("unused")
					AnchorLayout loginButtonLayout = new AnchorLayout();
					optionsBar.add(loginButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.NORTHWEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					loginButton.setLayout(null);
					loginButton.setText("Log In");
					loginButton.setFont(new java.awt.Font("Tahoma",1,16));
					loginButton.addActionListener(loginHandler(this));
				}
				{
					registerButton = new JButton();
					optionsBar.add(registerButton, new GridBagConstraints(1, 0, 1, 1, 0.0, 0.0, GridBagConstraints.SOUTHEAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					registerButton.setText("Register");
					registerButton.setFont(new java.awt.Font("Tahoma",1,16));
					registerButton.addActionListener(registerHandler(this));
				}
				{
					searchButton = new JButton();
					optionsBar.add(searchButton, new GridBagConstraints(0, 2, 2, 1, 0.0, 0.0, GridBagConstraints.NORTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					searchButton.setLayout(null);
					searchButton.setText("Search");
					searchButton.setFont(new java.awt.Font("Tahoma",1,16));
					searchButton.addActionListener(searchHandler(this));
				}
				{
					newMsgButton = new JButton();
					optionsBar.add(newMsgButton, new GridBagConstraints(0, 4, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					newMsgButton.setLayout(null);
					newMsgButton.setText("New Message");
					newMsgButton.setFont(new java.awt.Font("Tahoma",1,16));
					newMsgButton.addActionListener(newMsgHandler(this));
				}
				{
					replyButton = new JButton();
					optionsBar.add(replyButton, new GridBagConstraints(0, 5, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					replyButton.setLayout(null);
					replyButton.setText("Reply Message");
					replyButton.setFont(new java.awt.Font("Tahoma",1,16));
					replyButton.addActionListener(replyMsgHandler(this));
				}
				{
					demoteButton = new JButton();
					optionsBar.add(demoteButton, new GridBagConstraints(0, 7, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					demoteButton.setLayout(null);
					demoteButton.setText("Demote User");
					demoteButton.setFont(new java.awt.Font("Tahoma",1,16));
					demoteButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							String username = JOptionPane.showInputDialog(null, "Enter user name to demote", "Demote Menu", JOptionPane.QUESTION_MESSAGE);
							hadeleDemote(username);
						}
					});
				}
				{
					promoteButton = new JButton();
					optionsBar.add(promoteButton, new GridBagConstraints(0, 9, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					promoteButton.setLayout(null);
					promoteButton.setText("Promote User");
					promoteButton.setFont(new java.awt.Font("Tahoma",1,16));
					promoteButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							String username = JOptionPane.showInputDialog(null, "Enter user name to promote", "Promote Menu", JOptionPane.QUESTION_MESSAGE);
							hadelePromote(username);
						}
					});
				}
				{
					deleteUserButton = new JButton();
					optionsBar.add(deleteUserButton, new GridBagConstraints(0, 11, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					deleteUserButton.setLayout(null);
					deleteUserButton.setText("Delete User");
					deleteUserButton.setFont(new java.awt.Font("Tahoma",1,16));
					deleteUserButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							String username = JOptionPane.showInputDialog(null, "Enter user name to delete", "Delete user Menu", JOptionPane.QUESTION_MESSAGE);
							hadeleDeleteUser(username);

						}
					});
				}
				{
					addTopicButton = new JButton();
					optionsBar.add(addTopicButton, new GridBagConstraints(0, 13, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					addTopicButton.setLayout(null);
					addTopicButton.setText("Add Topic");
					addTopicButton.setFont(new java.awt.Font("Tahoma",1,16));
					//addTopicButton.addActionListener(actionListener_IL1);
				}
				{
					updateUserButton = new JButton();
					optionsBar.add(updateUserButton, new GridBagConstraints(0, 15, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					updateUserButton.setLayout(null);
					updateUserButton.setText("Update Details");
					updateUserButton.setFont(new java.awt.Font("Tahoma",1,16));
					updateUserButton.addActionListener(updatePassHandler(this));
				}
				{
					LogoffButton = new JButton();
					optionsBar.add(LogoffButton, new GridBagConstraints(0, 17, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					LogoffButton.setText("Log Out");
					LogoffButton.setFont(new java.awt.Font("Tahoma",1,16));
					LogoffButton.addActionListener(new ActionListener(){
						public void actionPerformed(ActionEvent e) {
							hadeleLogOut();
						}
					});

				}
			}
			{
				mainView = new JScrollPane();
				getContentPane().add(mainView, new GridBagConstraints(1, 1, 3, 2, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				{
					itemsContainer = new JPanel();
					BoxLayout itemsContainerLayout = new BoxLayout(itemsContainer, javax.swing.BoxLayout.Y_AXIS);
					mainView.setViewportView(itemsContainer);
					itemsContainer.setLayout(itemsContainerLayout);
				}
			}
			{
				navagationBar = new JPanel();
				GridBagLayout jPanel1Layout = new GridBagLayout();
				jPanel1Layout.columnWidths = new int[] {7, 7, 7, 7};
				jPanel1Layout.rowHeights = new int[] {7};
				jPanel1Layout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
				jPanel1Layout.rowWeights = new double[] {0.1};
				getContentPane().add(navagationBar, new GridBagConstraints(1, 3, 3, 3, 0.0, 0.0, GridBagConstraints.SOUTH, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
				navagationBar.setLayout(jPanel1Layout);
				{
					backButton = new JButton();
					@SuppressWarnings("unused")
					AnchorLayout backButtonLayout = new AnchorLayout();
					navagationBar.add(backButton, new GridBagConstraints(0, 0, 1, 1, 0.0, 0.0, GridBagConstraints.WEST, GridBagConstraints.VERTICAL, new Insets(0, 0, 0, 0), 0, 0));
					backButton.setLayout(null);
					backButton.setIcon(new ImageIcon(getClass().getClassLoader().getResource("presentation/images/backButtonImg.jpeg")));
					backButton.setSize(70, 70);
					backButton.setPreferredSize(new java.awt.Dimension(70, 70));
					backButton.addActionListener(backButtonHandler(this));
				}
				{
					usersStatus = new JLabel();
					GridBagLayout usersStatusLayout = new GridBagLayout();
					navagationBar.add(usersStatus, new GridBagConstraints(1, 0, 2, 1, 0.0, 0.0, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
					usersStatusLayout.rowWeights = new double[] {0.1, 0.1, 0.1, 0.1};
					usersStatusLayout.rowHeights = new int[] {7, 7, 7, 7};
					usersStatusLayout.columnWeights = new double[] {0.1, 0.1, 0.1, 0.1};
					usersStatusLayout.columnWidths = new int[] {7, 7, 7, 7};
					usersStatus.setLayout(usersStatusLayout);
					usersStatus.setFont(new java.awt.Font("Tahoma",1,12));
				}
			}
			visibleButtonsFor(0); // show buttons for guest users
			pack();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}


	/**
	 *
	 */
	public void connect(String hostname, int port) {
		try {
			this._connection.connect(hostname, port);
		} catch (UnknownHostException e) {
			System.out.println(e.getMessage());
		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
	}

	/**
	 *
	 */
	public void showWelcomePage() throws IOException {
		this.setVisible(true);
		TopicPacket rootTopic = this._connection.getRootTopic();
		this.updateMainView(rootTopic);
	}


	/**
	 *
	 */
	public void createItemsView(TopicPacket topics) {
		Vector<String> topicsNames = topics.getSubItems();
		for (int i = 0; i < topicsNames.size(); i++) {
			JButton tJButton = new JButton(topicsNames.elementAt(i));
			tJButton.setMaximumSize(new Dimension(600, 40));
			tJButton.addActionListener(this.topicHandler(this, i+1));
			this.itemsContainer.add(tJButton);
		}
	}


	/**
	 *
	 */
	public void createItemsView(ThreadPacket thread) {
		Vector<PostPacket> tPosts = thread.getPosts();
		int rank = this._connection.getUserRank();
		int index=0;
		for (PostPacket postPacket : tPosts) {
			PostView tPost;
			if(rank>1 & postPacket.getScore().equals("")){
				tPost = new PostView(this,index,postPacket.getSubject(), postPacket.getOwner(), postPacket.getContent(),postPacket.getDate(),true,true);
			}
			else if(rank>0 & postPacket.getScore().equals("") && postPacket.getOwner().equals(((Member)this._connection.getUser()).getUserName())){
				tPost = new PostView(this,index,postPacket.getSubject(), postPacket.getOwner(), postPacket.getContent(),postPacket.getDate(),true,false);
			}
			else{
				tPost = new PostView(this,index,postPacket.getSubject(), postPacket.getOwner(), postPacket.getContent(),postPacket.getDate(),false,false);
				if (!postPacket.getScore().equals(""))
					tPost.addScore(postPacket.getScore());
			}

			this.itemsContainer.add(tPost);
			index++;
		}

	}


	/**
	 * @param item
	 * update the MainView window to show the new Item
	 * does so by replacing the JPanel of the MainView with a new one
	 * and in that new Panel we put the Graphics for the Item (by calling item.accept(this))
	 */
	private void updateMainView(Item item) {
		this.itemsContainer.setVisible(false);
		this.itemsContainer = new JPanel();
		BoxLayout tLayout = new BoxLayout(this.itemsContainer, javax.swing.BoxLayout.Y_AXIS);
		this.itemsContainer.setLayout(tLayout);
		this._connection.setCurrentlyViewedItem(item);
		item.accept(this);
		visibleButtonsFor(this._connection.getUserRank());
		mainView.setViewportView(itemsContainer);
		this.itemsContainer.setVisible(true);
		this.setUserStatus(this._connection.getStatus());
	}

	/**
	 *
	 * @param form
	 * @param name
	 * @param password
	 */
	public void login(LoginForm form, String name, String password) {
		if (_connection.login(name, password)) {
			int rank = _connection.getUserRank();
			String username = ((Member)_connection.getUser()).getUserName();
			switch(rank){
			case 1: this.userName.setText(username+ " (Member)");break;
			case 2: this.userName.setText(username+ " (Moderador)");break;
			case 3: this.userName.setText(username+ " (Administrator)");break;
			case 4: this.userName.setText(username+ " (Master)");break;
			}
			visibleButtonsFor(rank);
			try {
				this.showWelcomePage();
			} catch (IOException e) {
			}
		}
		else {
			JOptionPane.showMessageDialog(form, "Wrong username/password.", "Check your details.", JOptionPane.ERROR_MESSAGE);
		}

	}

	/**
	 * make the right buttons appear for every user rank
	 * for guest - only login,register,search
	 * for member - plus write,reply,modify.
	 * for moderator - plus delete message
	 * for administrator - plus promote users, delete users, add and edit topics
	 * @param rank
	 */
	private void visibleButtonsFor(int rank) {
		boolean visible = false;
		if (rank >= 0){
			updateUserButton.setVisible(visible);
			addTopicButton.setVisible(visible);
			deleteUserButton.setVisible(visible);
			promoteButton.setVisible(visible);
			demoteButton.setVisible(visible);
			replyButton.setVisible(visible);
			newMsgButton.setVisible(visible);
			LogoffButton.setVisible(visible);
		}
		visible = true;
		if (rank >= 1){
			LogoffButton.setVisible(visible);
			updateUserButton.setVisible(visible);
			if(this._connection.getCurrentlyViewedItem() instanceof ThreadPacket) replyButton.setVisible(visible);
			if(this._connection.getCurrentlyViewedItem() instanceof TopicPacket) newMsgButton.setVisible(visible);
		}
		if (rank == 3){
			deleteUserButton.setVisible(visible);
			promoteButton.setVisible(visible);
			demoteButton.setVisible(visible);
		}
	}


	/**
	 * return an action listener. this action listener is good for Buttons who
	 * represent a Topic
	 * the action Listener gets the item we want to display
	 * and call the GUI to update its display with the new Item
	 * @param gui - the gui object
	 * @param choiceNum - number of the button that is pressed
	 * @return the Action Listener
	 */
	private ActionListener topicHandler(final WsepGUI gui, final int choiceNum) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				Item tChosen = gui._connection.getItem(choiceNum);
				gui.updateMainView(tChosen);
			}
		};
	}

	/**
	 *
	 * @param wsepGUI
	 * @return
	 */
	private ActionListener registerHandler(final WsepGUI gui) {
		return new ActionListener(){
			public void actionPerformed(ActionEvent arg0) {
				startRegister(gui);
			}
		};
	}

	/**
	 *
	 * @param gui
	 */
	public void startRegister(WsepGUI gui){
		@SuppressWarnings("unused")
		RegisterForm form = new RegisterForm(gui);
	}


	/**
	 *
	 * @param username
	 */
	private void hadeleDemote(String username) {
		if(this._connection.demoteUser(username)){
			JOptionPane.showMessageDialog(null, username + " was demoted", "Mission done", JOptionPane.INFORMATION_MESSAGE);
		}
		else{
			JOptionPane.showMessageDialog(null, "Could not demote this user", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 *
	 * @param username
	 */
	private void hadelePromote(String username) {
		if(this._connection.PromoteUser(username)){
			JOptionPane.showMessageDialog(null, username + " was promoted", "Mission done", JOptionPane.INFORMATION_MESSAGE);
		}
		else{
			JOptionPane.showMessageDialog(null, "Could not promote this user", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 *
	 * @param username
	 */
	private void hadeleDeleteUser(String username) {
		if(this._connection.deleteUser(username)){
			JOptionPane.showMessageDialog(null, username + " is history", "Mission done", JOptionPane.INFORMATION_MESSAGE);
		}
		else{
			JOptionPane.showMessageDialog(null, "Could not delete this user", "Error", JOptionPane.ERROR_MESSAGE);
		}
	}


	/**
	 * return an action listener. this action listener is good for the
	 * 'back' button. it gets the father item of the currently viewed item
	 * and call the GUI to update the display to that item (the father)
	 * @param gui - the gui object
	 * @return the action listener
	 */
	private ActionListener backButtonHandler(final WsepGUI gui) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				Item father = gui._connection.getFather();
				gui.updateMainView(father);
			}
		};
	}

	/**
	 *
	 * @param gui
	 * @return
	 */
	private ActionListener loginHandler(final WsepGUI gui) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				@SuppressWarnings("unused")
				LoginForm tLogin = new LoginForm(gui);
			}
		};
	}

	/**
	 *
	 * @param wsepGUI
	 * @return
	 */
	private ActionListener updatePassHandler(final WsepGUI gui) {
		return new ActionListener() {
			public void actionPerformed(ActionEvent event) {
				@SuppressWarnings("unused")
				UpdateUserForm from = new UpdateUserForm(gui,((Member)get_connection().getUser()).getUserName());
			}
		};
	}

	private void hadeleLogOut() {
		this._connection.logout();
		this.userName.setText("Guest");
		visibleButtonsFor(0);
		updateMainView(this._connection.getCurrentlyViewedItem());
	}

	/**
	 *
	 * @param gui
	 * @return
	 */
	private ActionListener searchHandler(final WsepGUI gui) {
		return new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				SearchMenu form = new SearchMenu(gui);
			}
		};
	}

	/**
	 *
	 * @param gui
	 * @return
	 */
	private ActionListener newMsgHandler(final WsepGUI gui) {
		return new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				NewMsgForm form = new NewMsgForm(gui);
			}
		};
	}

	/**
	 *
	 * @param gui
	 * @return
	 */
	private ActionListener replyMsgHandler(final WsepGUI gui) {
		return new ActionListener(){
			public void actionPerformed(ActionEvent e) {
				@SuppressWarnings("unused")
				ReplyMsgForm form = new ReplyMsgForm(gui);
			}
		};
	}

	/**
	 *
	 * @param me
	 * @param name
	 * @param pwd
	 */
	public void register(RegisterForm me, String name, String email, String pwd) {

		if(name.length()>2 && name.length()<20 && pwd.length() > 2 && pwd.length() <13){
			if(_connection.registerUser(name, email, pwd)){
				JOptionPane.showMessageDialog(me, "You are now registered as "+ name, "Welcome!", JOptionPane.INFORMATION_MESSAGE);
				login(null, name, pwd);
			}
			else{
				JOptionPane.showMessageDialog(me, name+" is already in the system.\n Choose another name", "Error!", JOptionPane.ERROR_MESSAGE);
				startRegister(this);
			}
		}
		else{
			JOptionPane.showMessageDialog(me, "Illegal username or password!", "Error!", JOptionPane.ERROR_MESSAGE);
			startRegister(this);
		}
	}

	/**
	 *
	 */
	public void handleSearchByContent(String content) {
		Vector<PostPacket> answer = this._connection.searchByContent(content);
		handleAnswer(answer);
	}

	/**
	 *
	 */
	public void handleSearchByDate(String fromDate, String untilDate) {
		Vector<PostPacket> answer = this._connection.searchByDate(fromDate, untilDate);
		/** stub
		Vector<Post> answer = new Vector<Post>();
		answer.add(new Post(null,null,"my subject", "my content"));
		answer.add(new Post(null,null,"search me", "bla bla bla"));
		**/
		handleAnswer(answer);
	}

	/**
	 *
	 */
	public void handleSearchByAuthor(String author) {
		Vector<PostPacket> answer = this._connection.searchByAuthor(author);
		handleAnswer(answer);
	}

	/**
	 * handling the creation of a Temporary Thread just for watching the
	 * search results that came from the search engine.
	 */
	private void handleAnswer(Vector<PostPacket> answer){
		//ForumThread tthread = new ForumThread(null);
		//tthread.setPosts(answer);
		//this.showItem(tthread);
		ThreadPacket tthread = new ThreadPacket();
		tthread.setFather(this._connection.getCurrentlyViewedItem());
		tthread.setPosts(answer);
		this.updateMainView(tthread);
	}

	/**
	 *
	 * @param form
	 * @param subj
	 * @param body
	 */
	public void addNewMessage(NewMsgForm form, String subj, String body) {
		_connection.addThread(subj, body);
		this.updateMainView((TopicPacket)_connection.getCurrentlyViewedItem());
		this.pack();
		this.repaint();
	}

	/**
	 *
	 * @param form
	 * @param subj
	 * @param body
	 */
	public void ReplyNewMessage(ReplyMsgForm form, String subj, String body) {
		_connection.addPost(subj, body);
		this.updateMainView((ThreadPacket)_connection.getCurrentlyViewedItem());
		this.pack();
		this.repaint();

	}

	/**
	 *
	 * @param post
	 * @param index
	 */
	public void startModify(PostView post, int index) {
		this._connection.setCurrentlyViewedItem(this._connection.getItem(index+1));
		@SuppressWarnings("unused")
		ModifyMsgForm form = new ModifyMsgForm(this,post.getSubject().getText(),post.getContent().getText());
	}

	/**
	 *
	 * @param modifyMsgForm
	 * @param newSubj
	 * @param newBody
	 */
	public void modifyMessage(ModifyMsgForm modifyMsgForm, String newSubj,
			String newBody) {
		this._connection.updatePost(newSubj, newBody);
		this._connection.setCurrentlyViewedItem(this._connection.getFather());
		this.updateMainView(this._connection.getCurrentlyViewedItem());
	}


	/**
	 *
	 * @param me
	 * @param oldPass
	 * @param newPass1
	 */
	public void updateUser(UpdateUserForm form, String newEmail,String oldPass, String newPass1) {
		if(newPass1.length() > 2 && newPass1.length() <13){
			if(_connection.updateUser(newEmail,oldPass, newPass1)){
				JOptionPane.showMessageDialog(form, "Your details changed successfuly", "", JOptionPane.INFORMATION_MESSAGE);
			}
			else{
				JOptionPane.showMessageDialog(form, "Problem occurd while trying to change your password", "Error!", JOptionPane.ERROR_MESSAGE);
			}
		}
		else{
			JOptionPane.showMessageDialog(form, "Illegal new password!", "Error!", JOptionPane.ERROR_MESSAGE);
		}
	}

	/**
	 *
	 * @param post
	 * @param postIndex
	 */
	public void HandleDeletePost(PostView post, int index) {
		this._connection.setCurrentlyViewedItem(this._connection.getItem(index+1));
		this._connection.deletePost();
		this._connection.setCurrentlyViewedItem(this._connection.getFather());
		this.updateMainView(this._connection.getCurrentlyViewedItem());
	}


	public UserConnection get_connection() {
		return _connection;
	}

	public void setFather() {
		this._connection.setCurrentlyViewedItem(this._connection.getFather());
	}

	public JButton getLogoffButton() {
		return LogoffButton;
	}

	public JLabel getUsersStatus() {
		return usersStatus;
	}

	public void setUserStatus(String stat){
		usersStatus.setText(stat);
	}

}
